// pages/index/index.ts
import { getWeather, backLogin } from "../../api/auth/user";
import { getListAll } from '../../api/site-monitoring'
import type { userInfo, siteList } from "../../type/type";
import { alarmList, PendingOrders } from '../../api/order'
const dayjs = require('../../utils/day.js');
const day = dayjs();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLeft: false as Boolean,
    userInfo: {} as userInfo,
    tabsName: "设备报警" as String,
    alarmList: [] as siteList[],
    robberyList: [],
    orderList: [],
    weatherInfo: {},
    month: '' as String,
    day: '' as String,
    week: '' as String
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // this.getAlarmList();
    // this.getRobberyList();
    // this.PendingOrders();
    // this.setData({
    //     userInfo:wx.getStorageSync("userInfo").data,
    //     month:dayjs().month()+1,
    //     day:dayjs().date(),
    //     week:this.setDate(dayjs().day())

    // })
    // let city = wx.getStorageSync("userInfo").data.city
    // getWeather(city).then((res:any)=>{
    //     this.setData({
    //         weatherInfo:res.data
    //     })
    //     // console.log(res)
    // })
  },
  isLogin() {
    wx.showLoading({
      title: '登录中...',
      mask: true
    })
    if (wx.getStorageSync("userInfo")) {
      wx.hideLoading()
    } else {
      wx.showModal({
        title: '登录失败',
        showCancel: false,
        confirmText: '确定',
        content: '用户登录信息失效,请重新登录',
        success: (res) => {
          if (res.confirm) {
            wx.reLaunch({
              url: '/pages/login/login'
            })
          }
        }
      })
      wx.hideLoading()
    }
  },
  async backLogin() {
    const res = await backLogin(this.data.userInfo.userID)
    if (res.data == 1) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
      wx.clearStorage()
    }else{
      wx.showToast({
        title:'退出失败',
        icon:'error'
      })
    }
  },
  handleMenu() {
    this.setData({
      isLeft: true,
    });
  },
  onClose() {
    this.setData({
      isLeft: false,
    });
  },
  onItem(e: any) {
    this.setData({
      tabsName: e.currentTarget.dataset.type,
    });
  },
  getAlarmList() {
    let comID = wx.getStorageSync("userInfo").data.comID
    let userID = wx.getStorageSync("userInfo").data.userID
    getListAll('1', '-1','', comID, userID, '').then((res: any) => {
      this.setData({
        alarmList: res.data.data
      })
      // console.log(res)
    })
  },
  getRobberyList() {
    let comID = wx.getStorageSync("userInfo").data.comID
    alarmList(comID).then((res: any) => {
      this.setData({
        robberyList: res.data.data
      })
    })
  },
  PendingOrders() {
    let comID = wx.getStorageSync("userInfo").data.comID
    let userID = wx.getStorageSync("userInfo").data.userID
    PendingOrders(comID, userID).then((res: any) => {
      this.setData({
        orderList: res.data.data
      })
    })
  },
  setDate(week: number) {
    let numCn = ['一', '二', '三', '四', '五', '六', '日'] as String[];
    if (week >= 1 && week <= 7) {
      return numCn[week - 1]
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.isLogin();
    this.getAlarmList();
    this.getRobberyList();
    this.PendingOrders();
    this.setData({
      userInfo: wx.getStorageSync("userInfo").data,
      month: dayjs().month() + 1,
      day: dayjs().date(),
      week: this.setDate(dayjs().day())

    })
    let city = wx.getStorageSync("userInfo").data.city
    getWeather(city).then((res: any) => {
      this.setData({
        weatherInfo: res.data
      })
      // console.log(res)
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
